<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-button size="default" type="primary" class="ml10" @click="add">
					<el-icon>
						<ele-Plus />
					</el-icon>
					添加
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="wxid" label="wxid" show-overflow-tooltip></el-table-column>
				<!-- <el-table-column prop="wxavata" label="头像" show-overflow-tooltip>
				<template #default="scope">
					<el-image
						style="width: 50px; height: 50px"
						:src="scope.row.wxavata"
						fit="cover"
					></el-image>
				</template>
				</el-table-column> -->
				<el-table-column prop="wxname" label="微信昵称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createdAt" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="130">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<addDeviceManager ref="addDeviceManagerRef" />
	</div>
</template>

<script setup lang="ts" name="systemUser">
import { reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useDevicemanager } from './devicemanager';
import { useUserInfo } from '/@/stores/userInfo';
import addDeviceManager from './addDeviceManager.vue';
import { storeToRefs } from 'pinia';
const devicemanagerStore = useDevicemanager();
const userStore = useUserInfo();
const { userInfo } = storeToRefs(userStore);
const addDeviceManagerRef = ref();
const state = reactive({
	tableData: {
		data: [],
		loading: false,
	},
});

const add = () => {
	addDeviceManagerRef.value.openDialog();
};

// 初始化表格数据
const getTableData = () => {
	if (!userInfo.value?.companyId) {
		ElMessage.error('获取用户企业信息失败。请重新登录后尝试');
		return;
	}
	state.tableData.loading = true;
	devicemanagerStore
		.getDeviceManagerList(userInfo.value?.companyId)
		.then((res) => {
			console.log(res.data);
			state.tableData.loading = false;
			state.tableData.data = res.data.list;
		})
		.catch((err) => {
			console.log(err);
			state.tableData.loading = false;
		});
};

// 删除用户
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将删除管理员：“${row.wxname}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			devicemanagerStore
				.removeDeviceManager(row.id)
				.then(() => {
					getTableData();
					ElMessage.success('删除成功');
				})
				.catch((err) => {
					ElMessage.error(err.message);
				});
		})
		.catch(() => {});
};

// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
